<template>
  <div>
    <van-nav-bar title="电卡充值记录" left-arrow left-text="返回" @click-left="$router.go(-1)" />
    <div class="block">
      按月选择：<span @click="popupShow = true">{{formatterMonth}} <van-icon name="arrow-down" size="12"/></span>
    </div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="loadData"
    >
      <van-cell
        v-for="item in list"
        :key="item.id"
        :title="'充值：' + item.amount + '元'"
        :value="'赠送：' + item.give + '元'"
        :label="'时间：' + item.createTime"
      >
        <span slot="icon" style="padding-right: 10px">
          <van-icon name="alipay" size="32" color="#09f" v-if="item.tranType === 2" />
          <van-icon name="wechat" size="32" color="#0a0" v-else />
        </span>
      </van-cell>
    </van-list>
    <van-popup
      v-model="popupShow"
      position="bottom"
    >
      <van-datetime-picker
        v-model="month"
        type="year-month"
        title="选择年月"
        :max-date="maxDate"
        :formatter="formatter"
        @cancel="popupShow = false"
        @confirm="reload"
      />
    </van-popup>
  </div>
</template>

<script>
import Utils from '../utils'
export default {
  name: 'CardRechargeRecord',
  data () {
    return {
      popupShow: false,
      month: new Date(),
      page: 1,
      loading: false,
      finished: false,
      list: [],
      cardNo: '',
      maxDate: new Date()
    }
  },
  computed: {
    formatterMonth () {
      const date = this.month
      return date.getFullYear() + '-' + (date.getMonth() + 1)
    }
  },
  created () {
    this.cardNo = this.$route.params.id
  },
  methods: {
    reload () {
      this.list = []
      this.finished = false
      this.page = 1
      this.popupShow = false
    },
    loadData () {
      this.$http.get('/restful/kxRechargeRecord/getUPage', {
        params: {
          cardNo: this.cardNo,
          page: this.page,
          limit: 20,
          tranStatus: 2,
          isUser: false,
          dates: Utils.DateMonth(this.month)
        }
      }).then(res => {
        if (res.code === 'ok') {
          this.list = this.list.concat(res.data.records)
          this.finished = res.data.current >= res.data.pages
          this.page = res.data.current + 1
          this.loading = false
        }
      })
    },
    formatter (type, val) {
      if (type === 'year') {
        return `${val}年`
      } else if (type === 'month') {
        return `${val}月`
      }
      return val
    }
  }
}
</script>

<style scoped lang='less'>

</style>
